﻿using SharpDX;
using SharpDX.Direct2D1;
using DW = SharpDX.DirectWrite;

namespace FadeGameFramework
{
    public class DebugDrawer : IUpdatable, IDrawer
    {
        private const int FONT_SIZE = 16;
        private const int ROW_WEIGHT = 500;
        private const int MAX_ROW_COUNT = 10;

        private readonly Brush defaultBrush;
        private readonly RectangleF[] defaultRectangleFList;
        private readonly DW.TextFormat defaultTextFormat;
        private float fps;

        public DebugDrawer(RenderTarget renderTarget) {
            var factory = new DW.Factory(DW.FactoryType.Shared);
            defaultBrush = new SolidColorBrush(renderTarget, Color.Black);
            defaultTextFormat = new DW.TextFormat(factory, "Microsoft Yahei Mono", FONT_SIZE);
            defaultRectangleFList = new RectangleF[MAX_ROW_COUNT];
            for (var i = 0; i < MAX_ROW_COUNT; i++) {
                defaultRectangleFList[i] = new RectangleF(0, i*FONT_SIZE, ROW_WEIGHT, FONT_SIZE);
            }
        }

        public void Draw() {
            var game = Game.Instance;
            var messages = new[] {
                $"FPS:{fps:F1}",
                $"TotalRunTime:{game.Dispatcher.TotalSeconds:F2}",
                $"SpritesCount:{game.SpriteManager.SpriteCount}",
                $"MousePosition:({game.InputManager.MousePositionX},{game.InputManager.MousePositionY})",
                $"IUpdatableCount:{game.Dispatcher.UpdatableListCount}"
            };
            for (var i = 0; i < messages.Length; i++) {
                game.RenderTarget.DrawText(messages[i], defaultTextFormat,
                    defaultRectangleFList[i], defaultBrush);
            }
        }

        public void Update(float dt) {
            fps = 1/dt;
        }
    }
}